package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    public SettingsVo settingsVo() {
        SettingsVo settingsVo = new SettingsVo();
        settingsVo.setId(UserHolder.getUserId());
        settingsVo.setPhone(UserHolder.getMobile());
        Question question = questionApi.findByUserId(UserHolder.getUserId());
        String text = question == null ? "我喜欢java" : question.getTxt();
        settingsVo.setStrangerQuestion(text);
        Settings settings = settingsApi.findByUserId(UserHolder.getUserId());
        if (settings != null) {
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }

        return settingsVo;
    }

    public void savatext(String content) {
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if (question == null) {
            Question questions = new Question();
            questions.setUserId(userId);
            questions.setTxt(content);
            questionApi.save(questions);
        } else {
            question.setTxt(content);
            questionApi.updata(question);
        }
    }

    public void savanotifications(Map map) {
        boolean likeNotification = (boolean) map.get("likeNotification");
        boolean pinglunNotification = (boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (boolean) map.get("gonggaoNotification");
        Long userId = UserHolder.getUserId();
        Settings settings = settingsApi.findByUserId(userId);
        if (settings == null) {
             settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.sava(settings);
        } else {
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.updata(settings);
        }
    }

    public PageResult blacklist(int page, int size) {
        Long userId = UserHolder.getUserId();
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId,page,size);
        PageResult pr = new PageResult(page,size,iPage.getTotal(),iPage.getRecords());
        return pr;
    }

    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.getUserId();
        blackListApi.delete(userId,blackUserId);
    }
}
